package org.springblade.upgrade.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.upgrade.config.entity.PackageBackendConfig;
import org.springblade.upgrade.config.model.PackageBackendConfigBean;
import org.springblade.upgrade.config.model.PackageFrontConfigBean;
import org.springblade.upgrade.config.service.IPackageBackendConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author qp
 */
@Slf4j
@RestController
@RequestMapping("/packageBackendConfig")
public class PackageBackendConfigController extends BladeController {

    @Resource
    private IPackageBackendConfigService packageBackendConfigService;

    /**
     * 新增接口
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody PackageBackendConfigBean param) {
        try {
            if (this.packageBackendConfigService.addData(param)) {
                return R.success("保存成功");
            }
            return R.fail("保存失败");
        } catch (Exception ex) {
            log.error("保存异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 编辑接口
     */
    @PostMapping("/edit")
    public R<String> edit(@RequestBody PackageBackendConfigBean param) {
        try {
            if (this.packageBackendConfigService.updateData(param)) {
                return R.success("修改成功");
            }
            return R.fail("修改失败");
        } catch (Exception ex) {
            log.error("修改异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 删除接口
     */
    @PostMapping("/remove")
    public R<String> remove(PackageBackendConfigBean param) {
        try {
            this.packageBackendConfigService.deleteData(param);
            return R.success("删除成功");
        } catch (Exception ex) {
            log.error("删除异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 查看详情接口
     */
    @GetMapping("/detail")
    public R<PackageBackendConfigBean> detail(PackageBackendConfigBean param) {
        try {
            PackageBackendConfigBean detail = this.packageBackendConfigService.findBySpec(param);
            return R.data(detail);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 查询分页
     */
    @GetMapping("/page")
    public R<IPage<PackageBackendConfigBean>> list(PackageBackendConfigBean param, Query query) {
        try {
            IPage<PackageBackendConfigBean> page = this.packageBackendConfigService.findPageBySpec(Condition.getPage(query), param);
            return R.data(page);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 查询分页
     */
    @GetMapping("/dicList")
    public R<List<Map<String, Object>>> dicList(PackageFrontConfigBean param) {
        try {
            List<PackageBackendConfig> beanList = packageBackendConfigService.list();
            List<Map<String, Object>> list = new ArrayList<>();
            if (beanList != null) {
                Map<String, Object> item;

                for (PackageBackendConfig data : beanList) {
                    item = new HashMap<>();
                    item.put("configId", data.getId());
                    item.put("configName", data.getSysModule());
                    list.add(item);
                }
            }
            return R.data(list);
        } catch (Exception ex) {
            log.error("查询异常" + ex.getMessage(), ex);
            return R.fail(ex.getMessage());
        }
    }


}

